# SIFT算子检测角点............................................
import cv2 as cv
import numpy as np
# 读取图像以及90度旋转图像
img = cv.imread(r'left08.jpg')
img1 = np.rot90(img)
# 创建检测算子以及提取描述子
surf = cv.xfeatures2d.SURF_create(100)
kp1, dst1 = surf.detectAndCompute(img, None)
kp2, dst2 = surf.detectAndCompute(img1, None)
# BFMatcher类对象的创建
BF = cv.BFMatcher(cv.NORM_L1)
#描述子匹配
matches = BF.match(dst1, dst2)
#画出100个描述子匹配点
img2 = cv.drawMatches(img, kp1, img1, kp2, matches[:100], None, flags = 2)
# 显示图像
cv.imshow("img", img2)
cv.waitKey()
ORB算子检测角点..................................................................
import cv2 as cv
# 读入左右反向图片
imgL = cv.imread(r'left.jpg')
imgR = cv.imread(r'right.jpg')
# 灰度图转换
grayL = cv.cvtColor(imgL, cv.COLOR_BGR2GRAY)
grayR = cv.cvtColor(imgR, cv.COLOR_BGR2GRAY)
# 创建orb算子以及提取描述子
orb = cv.ORB_create()
kpL, dstL = orb.detectAndCompute(grayL, None)
kpR, dstR = orb.detectAndCompute(grayR, None)
# 创建FBMatcher类对象，并且进行匹配
BF = cv.BFMatcher(cv.NORM_L2)
matches = BF.match(dstL, dstR)
# 显示orb匹配结果
img1 = cv.drawMatches(imgL, kpL, imgR, kpR, matches, None, flags=2)
cv.imshow("ORB", img1)
# 初始化Bruteforce匹配器
bf = cv.BFMatcher()
# 通过KNN匹配两张图片的描述子
matches = bf.knnMatch(dstL, dstR, k=2)
# 筛选比较好的匹配点
good = []
for i, (m, n) in enumerate(matches):
    if m.distance < 0.8 * n.distance:
        good.append(m)
# 画出匹配点
img2 = cv.drawMatches(imgL, kpL, imgR, kpR, good, None, flags=2)
cv.imshow("ORB-BF", img2)
cv.waitKey()
cv.destroyAllWindows()
#FAST算子检测角点..................................................................
import cv2 as cv
# 加载图片
imgL = cv.imread('A.jpg')
imgR = cv.imread('B.jpg')
# 转换为灰度图
grayL = cv.cvtColor(imgL, cv.COLOR_BGR2GRAY)
grayR = cv.cvtColor(imgR, cv.COLOR_BGR2GRAY)
# 提取特征点
fast = cv.FastFeatureDetector_create(50)
kL = fast.detect(grayL, None)
kR = fast.detect(grayR, None)
# 提取描述子
br = cv.BRISK_create()
kL, dL = br.compute(grayL, kL)
kR, dR = br.compute(grayR, kR)
# 创建 BFMatcher 对象
bf = cv.BFMatcher(cv.NORM_L2)
# 根据描述子匹配特征点.
matches = bf.match(dL, dR)
# 画出匹配点
img3 = cv.drawMatches(imgL, kL, imgR, kR, matches, None, flags=2)
cv.imshow("FAST", img3)
# 初始化Bruteforce匹配器
bf = cv.BFMatcher()
# 通过KNN匹配两张图片的描述子
matches = bf.knnMatch(dL, dR, k=2)
# 筛选比较好的匹配点
good = []
for i, (m, n) in enumerate(matches):
    if m.distance < 0.6 * n.distance:
        good.append(m)
# 画出匹配点
img3 = cv.drawMatches(imgL, kL, imgR, kR, good, None, flags=2)
cv.imshow("FAST-BF", img3)
cv.waitKey()
